<?php
declare (strict_types = 1);

namespace app\api\controller;

use think\facade\Db;
use app\common\model\House as HouseModel;
use app\common\model\People as PeopleModel;
use app\common\validate\House as validateInfo;

/**
 * @title 首页
 * @description 接口说明
 * //@group 接口分组
 * //@header name:key require:1 default: desc:秘钥(区别设置)
 * //@param name:public type:int require:1 default:1 other: desc:公共参数(区别设置)
 */
class House extends Base
{
    private $user=[];

    function __construct(){
        parent::__construct();
        $param = input('param.');
        $token = isset($param['token'])?$param['token']:'';
        if($token) {
            $res = checkToken($param['token']);
            if ($res['code']==2) {
                return dyajaxReturn(-1, $res['msg']);
            }
            $this->user = json_decode($res['data'], true);
        }
    }

    public function list(){
        $params = input('param.');
        $Model = new HouseModel();
        $user_id = $this->user['user_id'];
        if($params['type'] == 1) {
            $params['userid'] = $user_id;
        }else{
            $PeopleModel = new PeopleModel();
            $hi_arr = $PeopleModel -> where("true_user_id","=",$user_id)->column("house_id");
            $params['house_str'] = implode(",",$hi_arr);
        }
        $res = $Model->tableData($params);
        return dyajaxReturn(1,'获取成功',$res['data'],'',$res['count']);
    }

    public function add(){
        $param = input("param.");

        $validate = new validateInfo;
        $result = $validate->scene('add')->batch(false)->check($param);
        if(!$result){
            return dyajaxReturn(0,$validate->getError());
        }

        $Model = new HouseModel();
        $param['act'] = "add";
        $param['userid'] = $this->user['user_id'];
        $res = $list = $Model ->handle($param);
        return dyajaxReturn($res['code'],$res['msg'],$res['data']);
    }


    public function edit(){
        $param = input("param.");

        $validate = new validateInfo;
        $result = $validate->scene('edit')->batch(false)->check($param);
        if(!$result){
            return dyajaxReturn(0,$validate->getError());
        }

        $Model = new HouseModel();
        $param['act'] = "edit";
        $param['userid'] = $this->user['user_id'];
        $res = $list = $Model ->handle($param);
        return dyajaxReturn($res['code'],$res['msg'],$res['data']);
    }


    public function getHouse(){
        $param = input("param.");

        $Model = new HouseModel();
        $res = $Model->info($param['id']);
        return dyajaxReturn($res['code'],$res['msg'],$res['data']);
    }

    public function delete(){
        $param = input("param.");
        $Model = new HouseModel();
        $res = $Model->deleted($param['id']);
        return dyajaxReturn($res['code'],$res['msg'],$res['data']);
    }

}
